package io.geekidea.springbootplus.system.controller;


import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysCartItem;
import io.geekidea.springbootplus.system.entity.SysUser;
import io.geekidea.springbootplus.system.service.SysCartItemService;
import io.geekidea.springbootplus.system.vo.SysCartItemQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/v1")
@Module("system")
@Api(value = "购物车管理API接口",tags = {"购物车管理"})
public  class SysCartItemController {

    @Resource
    private SysCartItemService sysCartItemService;

    @GetMapping("/cart/list/{userId}")
    @OperationLog(name="从购物车中获取商品列表",type= OperationLogType.LIST)
    @ApiOperation(value="从购物车中获取商品列表",response = SysCartItemQueryVo.class)
    public ApiResult<List<SysCartItemQueryVo>> getSysCartListByUserId(@PathVariable(value ="userId")Integer userId)throws Exception{
    List<SysCartItemQueryVo> cartItems= sysCartItemService.findCartItemsByUserId(userId);
    return ApiResult.ok(cartItems);
    }



    @PostMapping( "/cart/add")
    @OperationLog(name = "将商品添加到购物车", type = OperationLogType.ADD)
    @ApiOperation(value ="加入购物车",response = ApiResult. class)
    public ApiResult<Boolean> addToCart(@Validated(Add.class) @RequestBody SysCartItem cartItem) throws Exception {
        boolean flag = sysCartItemService.addToCart(cartItem);
        return ApiResult.result(flag);

        }

    @PostMapping("/cart/delete/{id}")
    @OperationLog(name = "删除指定购物项", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除系统用户", response = ApiResult.class)
    public ApiResult<Boolean> deleteOneCartItem(@PathVariable("id")Integer itemId) throws Exception {
        boolean flag = sysCartItemService.deleteFromCart(itemId);
        return ApiResult.result(flag);
    }

    @PostMapping("/cart/delete/batch")
    @OperationLog(name = "清空购物车", type = OperationLogType.DELETE_BATCH)
    @ApiOperation(value = "清空购物车", response = ApiResult.class)
    public ApiResult<Boolean> deleteAllCartItem(@RequestBody List<Integer>ids) throws Exception {
        boolean flag = sysCartItemService.deleteAllFromCart(ids);
        return ApiResult.result(flag);
    }


    @PostMapping("/cart/update/{productId}")
    @OperationLog(name = "更新购物车中商品数量", type = OperationLogType.UPDATE)
    @ApiOperation(value = "更新购物车中商品数量", response = ApiResult.class)
    public ApiResult<Boolean> updateCartByProductId(@PathVariable(value = "productId") Integer id) throws Exception {
        boolean flag = sysCartItemService.updateCartItemByProductId(id);
        return ApiResult.result(flag);
    }
}
